package com.resp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/Servlet004")
public class Servlet04 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("执行了servlet04....");
        //设置响应体内容
        /*PrintWriter writer = resp.getWriter();
        writer.write("tom");
        resp.getWriter().write("<font size='10' color='red'>tomm</font>");*/

        /*//设置服务器写出编码为utf-8
        resp.setCharacterEncoding("utf-8");//如果单单这样写还是会有乱码的情况，要结合下面的header使用
        //告诉浏览器用什么编码方式来解析服务器传过来的数据
        resp.setHeader("Content-Type", "text/html;charset=utf-8");
        resp.getWriter().write("哈哈哈");*/

        /*//但下面的内容不需要和上面的一起使用
        resp.setHeader("Content-Type", "text/html;charset=utf-8");
        resp.getWriter().write("哈哈哈");*/

        resp.setContentType("text/html;charset=utf-8");
        resp.getWriter().write("哈哈哈");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
